/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package index;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import main.TestElementHandler;
import main.TestOnOneDoc;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import sql.SQLInterface;

/**
 *
 * @author Lu
 */
public class Index {

    public static String[] tables = new String[5];

    private static void initTables() {
        tables[4] = "document";
        tables[3] = "element";
        tables[2] = "word";
        tables[1] = "wordposition";
        tables[0] = "statdocelement";
    }

    public static void index(String[] documents) {

        try {
            initTables();

            SQLInterface.init();
            SQLInterface.cleanDatabase(tables);

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser;
            parser = factory.newSAXParser();
            DefaultHandler handler = new ElementHandler();

            for (String file : documents) {
                if (file != null) {
                    indexOneFile(file, parser, handler);
                    System.out.println("Parsed \"" + file + "\".");
                }
            }

//    for(Word w : ((ElementHandler) handler).getWordIndex()){
//                System.out.println(w.getName());
//            }
            indexElements(handler);
            System.out.println("Elements indexed.");
            indexWords(handler);
            System.out.println("Words indexed.");
            indexStats(handler);
            System.out.println("Statistics indexed.");

        } catch (SQLException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void indexOneFile(String fileName, SAXParser parser, DefaultHandler handler) {


        try {
            File fichier = new File(fileName);
            ((ElementHandler) handler).initNewFile(fichier);
            parser.parse(fichier, handler);

//            InputStreamReader isr = new InputStreamReader(new FileInputStream(fichier), "ISO-8859-15");
//            InputSource is = new InputSource(isr);
//            parser.parse(is, handler);

            SQLInterface.addDocument(((ElementHandler) handler).getDocumentName());

//            indexElementTypes(handler);

        } catch (SQLException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void indexElementTypes(DefaultHandler handler) {
        for (String elementType : ((ElementHandler) handler).getTypeList()) {
            try {
                SQLInterface.addElementType(elementType);

            } catch (SQLException ex) {
                Logger.getLogger(Index.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void indexElements(DefaultHandler handler) {
        for (Element element : ((ElementHandler) handler).getElementList()) {
            try {
                SQLInterface.addElement(element.getXPath(), element.getType(), element.getDocument());
            } catch (SQLException ex) {
                Logger.getLogger(Index.class
                        .getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static void indexWords(DefaultHandler handler) {
        for (Word word : ((ElementHandler) handler).getWordIndex()) {
            try {
                SQLInterface.addWord(word.getName(), word.getCounter(), word.getFrequency(), word.getWeight());
            } catch (SQLException ex) {
                Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
            }
            for (FullWord fullWord : word.getOccurences()) {
                try {
                    SQLInterface.addFullWord(word.getName(), fullWord.getFullName(), fullWord.getDocument(), fullWord.getElement(), fullWord.getPosition());
                } catch (SQLException ex) {
                    Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        }
    }

     public static void indexStats(DefaultHandler handler) {
//         SQLInterface.addElementStats(((ElementHandler) handler).getStats());
         for (ArrayList<StatsWordElement> l : ((ElementHandler) handler).getStats()) {
                for (StatsWordElement w : l) {
                 try {
                     SQLInterface.addElementStat(w.getDocument(), w.getElement(), w.getWord(), w.getCounter(), w.getTf(), w.getIdf());
                 } catch (SQLException ex) {
                     Logger.getLogger(Index.class.getName()).log(Level.SEVERE, null, ex);
                 }
                }
            }
    }
}
